package com.ruoyi.temu.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.temu.domain.TemuSubPurchaseOrderSku;
import com.ruoyi.temu.mapper.TemuSubPurchaseOrderMapper;
import com.ruoyi.temu.domain.TemuSubPurchaseOrder;
import com.ruoyi.temu.service.ITemuSubPurchaseOrderService;

/**
 * 备货单Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-25
 */
@Service
public class TemuSubPurchaseOrderServiceImpl implements ITemuSubPurchaseOrderService 
{
    @Autowired
    private TemuSubPurchaseOrderMapper temuSubPurchaseOrderMapper;

    /**
     * 查询备货单
     * 
     * @param subPurchaseOrderSn 备货单主键
     * @return 备货单
     */
    @Override
    public TemuSubPurchaseOrder selectTemuSubPurchaseOrderBySubPurchaseOrderSn(String subPurchaseOrderSn)
    {
        return temuSubPurchaseOrderMapper.selectTemuSubPurchaseOrderBySubPurchaseOrderSn(subPurchaseOrderSn);
    }

    /**
     * 查询备货单列表
     * 
     * @param temuSubPurchaseOrder 备货单
     * @return 备货单
     */
    @Override
    public List<TemuSubPurchaseOrder> selectTemuSubPurchaseOrderList(TemuSubPurchaseOrder temuSubPurchaseOrder)
    {
        return temuSubPurchaseOrderMapper.selectTemuSubPurchaseOrderList(temuSubPurchaseOrder);
    }

    /**
     * 新增备货单
     * 
     * @param temuSubPurchaseOrder 备货单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTemuSubPurchaseOrder(TemuSubPurchaseOrder temuSubPurchaseOrder)
    {
        int rows = temuSubPurchaseOrderMapper.insertTemuSubPurchaseOrder(temuSubPurchaseOrder);
        insertTemuSubPurchaseOrderSku(temuSubPurchaseOrder);
        return rows;
    }

    /**
     * 修改备货单
     * 
     * @param temuSubPurchaseOrder 备货单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTemuSubPurchaseOrder(TemuSubPurchaseOrder temuSubPurchaseOrder)
    {
        temuSubPurchaseOrderMapper.deleteTemuSubPurchaseOrderSkuBySubPurchaseOrderSn(temuSubPurchaseOrder.getSubPurchaseOrderSn());
        insertTemuSubPurchaseOrderSku(temuSubPurchaseOrder);
        return temuSubPurchaseOrderMapper.updateTemuSubPurchaseOrder(temuSubPurchaseOrder);
    }

    /**
     * 批量删除备货单
     * 
     * @param subPurchaseOrderSns 需要删除的备货单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTemuSubPurchaseOrderBySubPurchaseOrderSns(String[] subPurchaseOrderSns)
    {
        temuSubPurchaseOrderMapper.deleteTemuSubPurchaseOrderSkuBySubPurchaseOrderSns(subPurchaseOrderSns);
        return temuSubPurchaseOrderMapper.deleteTemuSubPurchaseOrderBySubPurchaseOrderSns(subPurchaseOrderSns);
    }

    /**
     * 删除备货单信息
     * 
     * @param subPurchaseOrderSn 备货单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTemuSubPurchaseOrderBySubPurchaseOrderSn(String subPurchaseOrderSn)
    {
        temuSubPurchaseOrderMapper.deleteTemuSubPurchaseOrderSkuBySubPurchaseOrderSn(subPurchaseOrderSn);
        return temuSubPurchaseOrderMapper.deleteTemuSubPurchaseOrderBySubPurchaseOrderSn(subPurchaseOrderSn);
    }

    /**
     * 新增备货单sku信息信息
     * 
     * @param temuSubPurchaseOrder 备货单对象
     */
    public void insertTemuSubPurchaseOrderSku(TemuSubPurchaseOrder temuSubPurchaseOrder)
    {
        List<TemuSubPurchaseOrderSku> temuSubPurchaseOrderSkuList = temuSubPurchaseOrder.getTemuSubPurchaseOrderSkuList();
        String subPurchaseOrderSn = temuSubPurchaseOrder.getSubPurchaseOrderSn();
        if (StringUtils.isNotNull(temuSubPurchaseOrderSkuList))
        {
            List<TemuSubPurchaseOrderSku> list = new ArrayList<TemuSubPurchaseOrderSku>();
            for (TemuSubPurchaseOrderSku temuSubPurchaseOrderSku : temuSubPurchaseOrderSkuList)
            {
                temuSubPurchaseOrderSku.setSubPurchaseOrderSn(subPurchaseOrderSn);
                list.add(temuSubPurchaseOrderSku);
            }
            if (list.size() > 0)
            {
                temuSubPurchaseOrderMapper.batchTemuSubPurchaseOrderSku(list);
            }
        }
    }
}
